import { createRouter, createWebHashHistory } from 'vue-router'
import { Toast } from "vant"
const routes = [
  {
    path: '/home',
    name: 'home',
    component: () => import('../views/home')
  },
  {
    path: '/cart',
    name: 'cart',
    component: () => import('../views/cart'),
    meta:{
      isAuth:true
    }
  },
  {
    path: '/mine',
    name: 'mine',
    component: () => import('../views/mine')
  },
  {
    path: '/order',
    name: 'order',
    component: () => import('../views/order'),
    meta:{
      isAuth:true
    }
  },
  {
    path: '/store',
    name: 'store',
    component: () => import('../views/store')
  },{
    path: '/createdorder',
    name: 'createdorder',
    component: () => import('../views/createdOrder'),
    meta:{
      isAuth:true
    }
  },
  {
    path:'/address',
    component:()=>import('../views/address'),
    meta:{
      isAuth:true
    }
  },
  {
    path:'/addressedit',
    component:()=>import('../views/addressEdit'),
    meta:{
      isAuth:true
    }
  },
  {
    path:'/userinfoedit',
    component:()=>import('../views/userinfoedit'),
    meta:{
      isAuth:true
    }
  },
  {
    path:'/login',
    component:()=>import('../views/login')
  },
  {
    path:'/register',
    component:()=>import('../views/register')
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})
router.beforeEach((to,from,next)=>{
  if(to.meta.isAuth){
    if (localStorage.isLogin==='login') {
      next()
    }else{
      next('/login')
      Toast('请先去登录')
    }
  }else{
    next()
  }
})

export default router
